package com.xc.h05.controller.weichen;

import com.github.pagehelper.PageInfo;
import com.xc.h05.entity.Reply;
import com.xc.h05.service.weichen.ReplyService;
import com.xc.h05.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/reply")
public class ReplyController {
    @Autowired
    private ReplyService replyService;

    @PostMapping("/add")
    public R add(@RequestBody Reply reply) {
        int add = replyService.add(reply);
        if (add != 0) {
            return R.ok(reply);
        } else {
            return R.fail("添加失败");
        }
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") int id) {
        int delete = replyService.delete(id);
        if (delete!= 0) {
            return R.ok("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }
    @PutMapping("/update")
    public R update(@RequestBody Reply reply) {
        int update = replyService.update(reply);
        if (update!= 0) {
            return R.ok("修改成功");
        } else {
            return R.fail("修改失败");
        }
    }
    @GetMapping("/findByPage")
    public R findByPage(int pageNum,int pageSize,Reply reply) {
        PageInfo<Reply> pageInfo = replyService.findByPage(pageNum, pageSize, reply);
        if (pageInfo!= null) {
            return R.ok(pageInfo);
        } else {
            return R.fail("查询失败");
        }
    }
    // 查询所有
    @GetMapping("/findAll")
    public R findAll() {
        List<Reply> list = replyService.findAll();
        if (list!= null) {
            return R.ok(list);
        } else {
            return R.fail("查询失败");
        }
    }
    // 根据qid查询所有回复的信息
    @GetMapping("/findByQid")
    public R findByQid(int qid) {
        List<Reply> list = replyService.findByQid(qid);
        if (list!= null) {
            return R.ok(list);
        } else {
            return R.fail("查询失败");
        }
    }

}
